package app;

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainConsole extends JFrame {
	
	public IVolatilityProvider volatilityAPI;
	IEarningsProvider earningsAPI = new ZacksEarningsProvider();

	public static void main(String[] args) {
		final MainConsole mainConsole = new MainConsole();
		
		mainConsole.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel buttonPanel = new JPanel( new GridLayout( 0, 1) );
		
		JButton butConnect = new JButton("Connect");
        butConnect.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e) {
            	// get connection parameters
                ConnectDlg dlg = new ConnectDlg(mainConsole);
                dlg.setVisible(true);
                if( !dlg.m_rc) {
                    return;
                }
                
                mainConsole.volatilityAPI = new IBVolatilityProvider(dlg.m_retIpAddress, dlg.m_retPort, dlg.m_retClientId);
            }
        });
        
        JButton butPlot = new JButton("Plot Earnings IV");
        butPlot.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e) {
            	// get connection parameters
                PlotDlg dlg = new PlotDlg(mainConsole);
                dlg.setVisible(true);
                if( !dlg.m_rc) {
                    return;
                }
                Map<EarningDate, List<SingleIVPoint>> ivData = null;
                try {
					Ticker ticker = mainConsole.earningsAPI.getTicker(dlg.m_retSymbol);
					ivData = mainConsole.volatilityAPI.getIVData(ticker, dlg.m_retDaysOut);
				} catch (ParseException | IOException e1) {
					e1.printStackTrace();
				}
                
                ChartDlg chartDlg = new ChartDlg(mainConsole, ivData);
                chartDlg.setVisible(true);
            }
        });
        
        buttonPanel.add(butConnect);
        buttonPanel.add(butPlot);
        
        mainConsole.getContentPane().add(buttonPanel);
        
        //Display the window.
        mainConsole.pack();
        mainConsole.setVisible(true);
	}

}
